package com.maxdu.labs.stt.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.example.stt.R;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.maxdu.labs.stt.constants.Constants;
import com.maxdu.labs.stt.fakes.FakeMenuActivity;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;

public class MainActivity extends Activity {

	//private Button enterBtn;
	private ProgressBar loading;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		Parse.initialize(this, "PZid04frw7DQqQgvEfY7vsL2BpzN0qPMrLIP0nf9", "vA8CGr4wIoHf1Hc12grfsJjR7sZfgzYoEzrWKvgb");
		ParseFacebookUtils.initialize("422311411242996");
		
		//hide status bar
		if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }else{
			ActionBar actionBar = getActionBar();
			actionBar.hide();
		}
		
		loading = (ProgressBar)findViewById(R.id.loading);
		loading.setVisibility(View.VISIBLE);
		
		//Initialize FB
		Session.openActiveSession(this, true, new Session.StatusCallback() {
		    @Override
		    public void call(Session session, SessionState state, Exception exception) {
		        if(session.isOpened()) {
		            Log.w(Constants.LOG_TAG, "FB session opened successfully");
		        } else {
		        	if(exception != null){
		        		Log.w(Constants.LOG_TAG, "FB session not opened ... " + exception.toString());
		        	}
		        }
		    }
		});
		
		ParseFacebookUtils.logIn(this, new LogInCallback() {
			  @Override
			  public void done(ParseUser user, ParseException err) {
			    if (user == null) {
			      Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
			    } else if (user.isNew()) {
			    	// make request to the /me API
			    	Request request = Request.newMeRequest(Session.getActiveSession(), new Request.GraphUserCallback() {

			    	  // callback after Graph API response with user object
			    	  @Override
			    	  public void onCompleted(GraphUser user, Response response) {
			    		  
							String facebookUsername = user.getName();
							if (facebookUsername != null){
								ParseUser.getCurrentUser().setUsername(facebookUsername);
								ParseUser.getCurrentUser().saveEventually();
							}
  
						Intent intent = new Intent(MainActivity.this,FakeMenuActivity.class);
						startActivity(intent);
			    	  }
			    	});
			    	request.executeAsync();
			    } else {
			    	Log.d("MyApp", "User logged in through Facebook!");
					Intent intent = new Intent(MainActivity.this,FakeMenuActivity.class);
					startActivity(intent);
			    }
			  }
			});
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  super.onActivityResult(requestCode, resultCode, data);
	  ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
	}
}
